package com.jgp.glmeeting.service.impl;

import com.jgp.glmeeting.ema.server.jwsserver.sms.*;
import com.jgp.glmeeting.ema.util.MD5;
import com.jgp.glmeeting.ema.util.SmsServiceFactory;
import com.jgp.glmeeting.ema.util.SmsStringUtils;
import com.jgp.glmeeting.model.Inform;
import com.jgp.glmeeting.model.Meeting;
import com.jgp.glmeeting.model.Participants;
import com.jgp.glmeeting.repository.InformRepository;
import com.jgp.glmeeting.service.ArrangementService;
import com.jgp.glmeeting.service.InformService;
import com.jgp.glmeeting.service.ParticipantsService;
import com.jgp.sys.common.query.Operator;
import com.jgp.sys.common.query.OrderList;
import com.jgp.sys.common.query.QueryFilterList;
import com.jgp.sys.ui.Pager;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class InformServiceImpl implements InformService {

    private final Logger logger = LoggerFactory.getLogger(InformServiceImpl.class);

    @Autowired
    private InformRepository informRepository;
    @Autowired
    private ParticipantsService participantsService;
    @Autowired
    private ArrangementService arrangementService;

    //查询所有
    @Override
    public List<Inform> queryInforms(Inform i, Pager pager) {
        OrderList orders = new OrderList();
        orders.addOrder("id","desc");
        String informTitle=i.getInformTitle();

        QueryFilterList queryFilterList=new QueryFilterList();
        //以下加限制条件
        if(StringUtils.isNotBlank(informTitle)){
            queryFilterList.addFilter("informTitle","like",informTitle);
        }
        return informRepository.read(queryFilterList,orders,pager);
    }

    //添加通知
    @Transactional
    @Override
    public void addInform(Inform i) {
       informRepository.createOrUpdate(i);
    }

    //删除通知
    @Transactional
    @Override
    public void removeInform(String id) {
        informRepository.delete(id);
    }

    @Override
    public Inform queryInformID(String id) {
        return informRepository.read(id);
    }

    @Override
    public List<Inform> queryListByMeetingIdAndParticipantsId(String meetingId, String participantsId) {
        List<Inform> list = informRepository.read(new QueryFilterList().addFilter("meetingID", Operator.eq, meetingId).addFilter("participantsId", Operator.eq, participantsId));
        return list;
    }

    @Transactional
    @Override
    public void addInformAndSendSms(Inform inform, Participants participants, Meeting meeting) throws Exception {
        //todo 添加完成的模板参数信息
        Map<String, String> map = new HashMap<>();
        map.put("meetingName", meeting.getName());


        //替换模板信息
        inform.setInformContent(SmsStringUtils.replace(inform.getInformContent(), map));
        informRepository.create(inform);

        //获取发送短信的服务
        SmsOperatorImpServiceLocator service = SmsServiceFactory.getInstance();
        //要发送短信的内容
        MtMessage msg = new MtMessage();
        msg.setContent(inform.getInformContent());
        msg.setPhoneNumber(new String[] {participants.getMobilePhone()});
        ISmsOperator operator = service.getSmsOperatorImpPort();
        MtMessageRes msgRes = operator.sendSms("gl96660", MD5.MD5Encode("lz96660"), "12", msg); // smsType12
        if (msgRes.getResDetail() != null) {
            for (MtMessageResDetail detail : msgRes.getResDetail()) {
                logger.info("发送短信的结果： phoneNumber: " + detail.getPhoneNumber() + "\t state: " + detail.getStat() + "\t state description: " + detail.getStatDes());
            }
        }
    }
}
